package com.fyx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fyx.entity.*;
import com.fyx.mapper.*;
import com.fyx.service.IMaiInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MaiInfoServiceImpl  extends ServiceImpl<SysDictDetailMapper, SysDictDetail> implements IMaiInfoService {
    @Autowired
    MaiInfoMapper maiInfoMapper;

    @Autowired
    MaiRoomsInfoMapper maiRoomsInfoMapper;

    @Autowired
    MaiAdInfoMapper maiAdInfoMapper;

    @Autowired
    MaiShareInfoMapper maiShareInfoMapper;

    @Override
    public List<MaiAdInfo> getMaiAdInfo(Integer time,String channel){
        QueryWrapper<MaiAdInfo> wrapper =new QueryWrapper<>();
        if(time!=null&&time>0){
            wrapper.eq("time",time);
            wrapper.eq("the_father",channel);
        }

        wrapper.orderBy(true,false,"id");
        List<MaiAdInfo> list = maiAdInfoMapper.selectList(wrapper);
        if(list.size()>0){
            for(MaiAdInfo maiAdInfo:list){
                if(maiAdInfo.getChannel().equals("h5hzmjxl_wechat")||maiAdInfo.getChannel().equals("h5hzmjxl_qq")||maiAdInfo.getChannel().equals("h5hcmjxl_wechat")||maiAdInfo.getChannel().equals("h5gdmjxl_wechat")) {
                    if(maiAdInfo.getTheFather().equals("h5hzmjxl_wechat")||maiAdInfo.getTheFather().equals("h5hzmjxl_qq")||maiAdInfo.getTheFather().equals("h5hcmjxl_wechat")||maiAdInfo.getTheFather().equals("h5gdmjxl_wechat")){
                        maiAdInfo.setHasChildren(false);
                    }
                }else if(maiAdInfo.getChannel().indexOf("_total")>0){

                }
                else {
                    maiAdInfo.setHasChildren(false);
                }
            }
        }
        return list;
    }
    @Override
    public  List<MaiRoomsInfo> getMaiRoomsInfo(Integer time,String channel){
        QueryWrapper<MaiRoomsInfo> wrapper =new QueryWrapper<>();
        if(time!=null&&time>0){
            wrapper.eq("time",time);
            wrapper.eq("the_father",channel);
        }
        wrapper.orderBy(true,false,"id");
        List<MaiRoomsInfo> list = maiRoomsInfoMapper.selectList(wrapper);
        if(list.size()>0){
            for(MaiRoomsInfo maiRoomsInfo:list){
                if(maiRoomsInfo.getChannel().equals("h5hzmjxl_wechat")||maiRoomsInfo.getChannel().equals("h5hzmjxl_qq")) {
                    if(maiRoomsInfo.getTheFather().equals("h5hzmjxl_wechat")||maiRoomsInfo.getTheFather().equals("h5hzmjxl_qq")){
                        maiRoomsInfo.setHasChildren(false);
                    }
                }else if(maiRoomsInfo.getChannel().indexOf("_total")>0){

                }
                else {
                    maiRoomsInfo.setHasChildren(false);
                }
            }
        }
        return list;
    }
    @Override
    public List<MaiInfo> getMaiInfo(Integer time,String channel){
        QueryWrapper<MaiInfo> wrapper =new QueryWrapper<>();
        if(time!=null&&time>0){
            wrapper.eq("time",time);
            wrapper.eq("the_father",channel);
        }
        wrapper.orderBy(true,false,"id");
        List<MaiInfo> list = maiInfoMapper.selectList(wrapper);
        if(list.size()>0){
            for(MaiInfo maiInfo:list){
                if(maiInfo.getChannel().equals("h5hzmjxl_wechat")||maiInfo.getChannel().equals("h5hzmjxl_qq")) {
                    if(maiInfo.getTheFather().equals("h5hzmjxl_wechat")||maiInfo.getTheFather().equals("h5hzmjxl_qq")){
                        maiInfo.setHasChildren(false);
                    }
                }else if(maiInfo.getChannel().indexOf("_total")>0){

                }else {
                    maiInfo.setHasChildren(false);
                }
            }
        }
        return list;
    }
    @Override
    public List<MaiShareInfo> getMaiShareInfo(Integer time,String channel){
        QueryWrapper<MaiShareInfo> wrapper =new QueryWrapper<>();
        if(time!=null&&time>0){
            wrapper.eq("time",time);
            wrapper.eq("the_father",channel);
        }
        wrapper.orderBy(true,false,"id");
        List<MaiShareInfo> list = maiShareInfoMapper.selectList(wrapper);
        if(list.size()>0){
            for(MaiShareInfo maiShareInfo:list){
                if(maiShareInfo.getChannel().equals("h5hzmjxl_wechat")||maiShareInfo.getChannel().equals("h5hzmjxl_qq")||maiShareInfo.getChannel().equals("h5hcmjxl_wechat")||maiShareInfo.getChannel().equals("h5gdmjxl_wechat")) {
                    if(maiShareInfo.getTheFather().equals("h5hzmjxl_wechat")||maiShareInfo.getTheFather().equals("h5hzmjxl_qq")||maiShareInfo.getTheFather().equals("h5hcmjxl_wechat")||maiShareInfo.getTheFather().equals("h5gdmjxl_wechat")){
                        maiShareInfo.setHasChildren(false);
                    }
                }else if(maiShareInfo.getChannel().indexOf("_total")>0){

                }else {
                    maiShareInfo.setHasChildren(false);
                }
            }

        }
        return list;
    }
    @Override
   public Map getMaiAdInfo(MaiAdInfo maiAdInfo, Pageable pageable){
       Page<MaiAdInfo> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
       QueryWrapper<MaiAdInfo> wrapper =new QueryWrapper<>();
       if(maiAdInfo.getQTime()!=null){
           wrapper.ge("time",maiAdInfo.getQTime().get(0).getTime()/1000);
           wrapper.le("time",maiAdInfo.getQTime().get(1).getTime()/1000);
       }
       if(maiAdInfo.getChannel()!=null){
           if(maiAdInfo.getTheFather()!=null){
               wrapper.eq("the_father",maiAdInfo.getTheFather());
               wrapper.eq("channel","h5gdmjxl_wechat");
           }else {
               wrapper.eq("channel", maiAdInfo.getChannel());
           }
       }else{
           wrapper.eq("channel","total").or().isNull("channel");

       }
       if(maiAdInfo.getTime()!=null&&maiAdInfo.getTime()>0){
           wrapper.eq("time",maiAdInfo.getTime());
           wrapper.ne("channel","total");
       }
       wrapper.orderBy(true,false,"time");
       IPage<MaiAdInfo> ipage = maiAdInfoMapper.selectPage(page,wrapper);
       List<MaiAdInfo> list = ipage.getRecords();
       System.out.println(list);
       long total = ipage.getTotal();
       Map map =new HashMap();
       map.put("list",list);
       map.put("total",total);
       return map;
   }
    @Override
    public Map getMaiRoomsInfo(MaiRoomsInfo maiRoomsInfo, Pageable pageable){
        Page<MaiRoomsInfo> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
        QueryWrapper<MaiRoomsInfo> wrapper =new QueryWrapper<>();
        if(maiRoomsInfo.getQTime()!=null){
            wrapper.ge("time",maiRoomsInfo.getQTime().get(0).getTime()/1000);
            wrapper.le("time",maiRoomsInfo.getQTime().get(1).getTime()/1000);
        }
        if(maiRoomsInfo.getChannel()!=null){
            wrapper.eq("channel",maiRoomsInfo.getChannel());
        }else{
            wrapper.eq("channel","total").or().isNull("channel");
        }
        if(maiRoomsInfo.getTime()!=null&&maiRoomsInfo.getTime()>0){
            wrapper.eq("time",maiRoomsInfo.getTime());
            wrapper.ne("channel","total");
        }
        wrapper.orderBy(true,false,"time");
        IPage<MaiRoomsInfo> ipage = maiRoomsInfoMapper.selectPage(page,wrapper);
        List<MaiRoomsInfo> list = ipage.getRecords();
        long total = ipage.getTotal();
        Map map =new HashMap();
        map.put("list",list);
        map.put("total",total);
        return map;
    }
    @Override
    public Map getMaiInfo(MaiInfo maiInfo, Pageable pageable){
    Page<MaiInfo> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
    QueryWrapper<MaiInfo> wrapper =new QueryWrapper<>();
       if(maiInfo.getQTime()!=null){
        wrapper.ge("time",maiInfo.getQTime().get(0).getTime()/1000);
        wrapper.le("time",maiInfo.getQTime().get(1).getTime()/1000);
    }
        if(maiInfo.getChannel()!=null){
            wrapper.eq("channel",maiInfo.getChannel());
        }else{
            wrapper.eq("channel","total").or().isNull("channel");

        }
        if(maiInfo.getTime()!=null&&maiInfo.getTime()>0){
            wrapper.eq("time",maiInfo.getTime());
            wrapper.ne("channel","total");
        }
       wrapper.orderBy(true,false,"time");
    IPage<MaiInfo> ipage = maiInfoMapper.selectPage(page,wrapper);
    List<MaiInfo> list = ipage.getRecords();
    long total = ipage.getTotal();
    Map map =new HashMap();
       map.put("list",list);
       map.put("total",total);
       return map;
   }
   @Autowired
    private BindPlayerMapper  bindPlayerMapper;

    @Override
    public Map getMaiShareInfo(MaiShareInfo maishareInfo, Pageable pageable){
        Page<MaiShareInfo> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
        QueryWrapper<MaiShareInfo> wrapper =new QueryWrapper<>();
        if(maishareInfo.getQTime()!=null){
            wrapper.ge("time",maishareInfo.getQTime().get(0).getTime()/1000);
            wrapper.le("time",maishareInfo.getQTime().get(1).getTime()/1000);
        }
        if(maishareInfo.getChannel()!=null){
            wrapper.eq("channel",maishareInfo.getChannel());
        }else{
            wrapper.eq("channel","total").or().isNull("channel");

        }
        if(maishareInfo.getTime()!=null&&maishareInfo.getTime()>0){
            wrapper.eq("time",maishareInfo.getTime());
            wrapper.ne("channel","total");
        }
        wrapper.orderBy(true,false,"time");
        IPage<MaiShareInfo> ipage = maiShareInfoMapper.selectPage(page,wrapper);
        List<MaiShareInfo> list = ipage.getRecords();
        long total = ipage.getTotal();
        BindPlayer bindPlayer=new BindPlayer();
        List<BindPlayer> list1=bindPlayerMapper.getBindList(bindPlayer);
        Map map =new HashMap();
        map.put("content",list);
        map.put("totalElements",total);
        map.put("countData",list1);
        return map;
    }
}
